<template>
  <div class="promotion">
    <div class="common">
      <div class="common-input">
        <el-input
          v-model="commonValue"
          placeholder="用户昵称"
          clearable
        ></el-input>
      </div>

      <div class="common-button" @click="searchHit">搜索</div>
      <div class="common-button" @click="replacementHit">重置</div>
    </div>

    <div class="table" v-loading="loading">
      <el-table
        tooltip-effect="dark"
        ref="multipleTable"
        :data="tableData"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
        :header-cell-style="{
          color: '#333333',
          backgroundColor: '#f3f3f3',
        }"
      >
        <el-table-column type="selection" width="70" align="center">
        </el-table-column>
        <el-table-column prop="promotionName" label="推广平台" width="200">
        </el-table-column>
        <el-table-column prop="nickname" label="推广人"> </el-table-column>
        <el-table-column prop="shopId" label="推广店铺ID"> </el-table-column>
        <el-table-column prop="" label="审核状态">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.promotionAuditStatus == 0">未审核</el-tag>
            <el-tag v-if="scope.row.promotionAuditStatus == 1" type="success"
              >已通过</el-tag
            >
            <el-tag v-if="scope.row.promotionAuditStatus == 2" type="error"
              >已否决</el-tag
            >
          </template>
        </el-table-column>
        <el-table-column prop="createTime" label="提交时间" width="200">
        </el-table-column>
        <el-table-column prop="promotionAuditBreak" label="备注" width="300">
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="scope">
            <div class="operation">
              <div class="list" @click.prevent="auditHit(scope.row)" v-if="scope.row.promotionAuditStatus == 0">审核</div>
              <div class="list" @click.prevent="detailsHit(scope.row)">
                详情
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>

      <div class="pagination">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
          layout="prev, pager, next"
          :total="tableDataTotal"
          :current-page="page"
        >
        </el-pagination>
      </div>
    </div>

    <!--新增或者编辑-->
    <div class="add">
      <el-dialog
        title="审核页面"
        :visible.sync="addVisible"
        :before-close="addClose"
        width="30%"
      >
        <div class="add-form">
          <el-form
            :model="ruleForm"
            ref="ruleForm"
            label-width="140px"
            class="demo-ruleForm"
          >
            <el-form-item label="推广内容" prop="icon">
              <div
                style="display: flex; flex-wrap: wrap"
                v-if="ruleForm.banner.length > 0"
              >
                <div
                  class="img"
                  v-for="(item, index) in ruleForm.banner"
                  :key="index"
                >
                  <img :src="item" alt="" />
                </div>
              </div>

              <div style="display: flex" v-else>
                <div class="img">暂无材料</div>
              </div>
            </el-form-item>

            <el-form-item label="备注" prop="remark">
              <el-input
                v-model="ruleForm.remark"
                placeholder="请输入备注"
                clearable
                type="textarea"
                :rows="4"
              ></el-input>
            </el-form-item>

            <el-form-item class="attachment">
              <div class="list" @click="addRefuse">拒绝</div>
              <div class="list" @click="addConfirm('ruleForm')">通过</div>
            </el-form-item>
          </el-form>
        </div>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  promotionList,
  promotionListId,
  promotionListAudit,
} from "@/api/promotion";

export default {
  data() {
    return {
      commonValue: "",
      //-------------
      page: 1,
      limit: 10,
      tableData: [],
      tableDataTotal: 0,
      loading: false,
      commonObj: {},
      multipleSelection: [],
      //-------------
      addVisible: false,
      addRoaid: 0,
      ruleForm: {
        banner: "",
        remark: "",
      },
      // rules: {
      //   icon: [{ required: true, message: "请输入推广内容", trigger: "blur" }],

      //   remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
      // },
    };
  },
  created() {
    this.promotionListMet();
  },
  methods: {
    promotionListMet() {
      let params = {
        nickname: this.commonValue,
        pageSize: this.limit,
        pageNo: this.page,
      };

      promotionList(params).then((res) => {
        if (res.errorCode == null) {
          this.tableData = res.resultData.welfarePromotionList;
          this.tableDataTotal = res.resultData.totalNum;
          this.loading = false;
        }
      });
    },
    //-------
    searchHit() {
      this.loading = true;
      this.promotionListMet();
    },
    replacementHit() {
      this.commonValue = "";
      this.loading = true;
      this.promotionListMet();
    },

    //--------------
    auditHit(item) {
      this.commonObj = item;
      if (item.promotionAuditImage != null) {
        this.ruleForm.banner = item.promotionAuditImage.split(";");
      } else {
        this.ruleForm.banner = [];
      }

      this.remark = item.promotionAuditBreak;

      this.addVisible = true;
    },

    detailsHit(item) {
      this.$router.push({
        path: "/promotion/list-details",
        query: {
          id: item.welfarePromotionId,
        },
      });
    },
    //-----
    handleAvatarSuccess(res, file) {
      this.ruleForm.icon = res.resultData;
    },
    //-----
    addClose() {
      this.addVisible = false;
    },
    addRefuse() {
      this.promotionListAuditMet(2);
    },
    addConfirm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.promotionListAuditMet(1);
        } else {
          return false;
        }
      });
    },

    promotionListAuditMet(index) {
      let params = {
        welfarePromotionId: this.commonObj.welfarePromotionId,
        promotionAuditStatus: index,
        promotionAuditBreak: this.ruleForm.remark,
      };
      promotionListAudit(params).then((res) => {
        if (res.errorCode == null) {
           this.loading = true;
          this.$message.success("审核成功！");
          this.addVisible = false;
          this.promotionListMet();
        }
      });
    },

    //----------------------
    handleSelectionChange(val) {
      this.multipleSelection = val;
    },
    //---------
    handleSizeChange(val) {
      this.limit = val;
      this.loading = true;
      this.promotionListMet();
    },
    handleCurrentChange(val) {
      this.page = val;
      this.loading = true;
      this.promotionListMet();
    },
  },
};
</script>

<style lang="less" scoped>
@import url("@/assets/css/common.less");

.promotion {
  .add {
    /deep/ .el-dialog__header {
      font-size: 16px;
      text-align: center;
    }

    /deep/ .el-dialog__body {
      padding-bottom: 10px;
    }

    .add-form {
      /deep/ .el-select {
        width: 100%;
      }

      .img {
        width: 100px;
        height: 100px;
        display: flex;
        align-items: center;
        justify-content: center;
        border: 1px solid #ccc;
        border-radius: 5px;
        margin-right: 10px;
        color: #cccccc;

        img {
          width: 100%;
          height: 100%;
          border-radius: 5px;
        }
      }

      .attachment {
        margin-top: 50px;
        /deep/ .el-form-item {
          margin-bottom: 0;
        }

        /deep/ .el-form-item__content {
          margin-left: 0 !important;
          display: flex;
          align-items: center;
          justify-content: center;
        }

        .list {
          background: #3da8f5;
          color: #fff;
          font-size: 16px;
          margin-right: 50px;
          border-radius: 5px;
          width: 100px;
          height: 35px;
          display: flex;
          align-items: center;
          justify-content: center;
          cursor: pointer;
        }

        .list:first-child {
          background: #808080;
        }

        .list:last-child {
          margin-right: 0;
        }
      }
    }
  }
}
</style>